package com.happypets.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.happypets.model.PageBean;
import com.happypets.model.PetBean;
import com.happypets.services.PetService;
import com.happypets.servicesImpl.PetServiceImpl;

public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	PetService petService = new PetServiceImpl();
	PageBean pageBean = new PageBean();
	int petKind = 1;//1:dog  2:cat  3:bird

	
	int flag = 0;
       
    public SearchServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("------searchServlet start");
		
		HttpSession session = request.getSession();
		
		Map<String,String> keyMap = new HashMap<String,String>();
		
		//最大页数
				int maxPageNumber = 1;
				//当前页
				int currentPageNumber = 1;
				//每页显示多少条
				int showNumber = 3;
				//一共多少
				int allNumber = 0;
				
				String temp = (String)request.getParameter("currentPageNumber");
				if(temp != null){
					currentPageNumber = Integer.parseInt(temp);
				}
				temp = (String)request.getParameter("showNumber");
				if(temp != null){
					showNumber = Integer.parseInt(temp);
					//currentPageNumber = 1;//返回到第一页
					
					System.out.println("showNumber"+temp);
					
				}
				if(request.getParameter("kind") != null){
					petKind = Integer.parseInt(request.getParameter("kind"));
				}
				
				String queryCount = "select a.id from animalsinfo a where a.bigcategoryid = "+petKind;
				
				String color = (String)session.getAttribute("color");
				String shape = (String)session.getAttribute("shape");
				String category = (String)session.getAttribute("category");
				String keywords = (String)session.getAttribute("keywords");
				
				System.out.println("color "+color+"shape "+shape+"category "+category+"keywords "+keywords);
				
				if(color != null && !color.equals("null")){
					flag = 1;
					 queryCount ="select t.* from animalsinfo t where t.bigcategoryid = "+petKind+" and t.animal_color='"+color+"'";
					 keyMap.put("color", color);
				}else{
					session.removeAttribute("color");
				}
				
				if(shape != null && !shape.equals("null")){
					flag = 1;
					 queryCount = "select t.* from animalsinfo t where t.bigcategoryid = "+petKind+" and t.animal_shape='"+shape+"'";
					keyMap.put("shape", shape);
				}else{
					session.getAttribute("shape");
				}
				
				if(category != null && !category.equals("null")){
					flag = 1;
					 queryCount = "select a.* from animalsinfo a right join (select smallcategory from smallcategory where name='"+category+"') b on a.smallcategoryid=b.smallcategory";
					 keyMap.put("category", category);
				}else{
					session.removeAttribute("category");
				}
				
				if(keywords != null && !keywords.equals("null")){
					flag = 1;
					 queryCount = "select a.id from animalsinfo a where a.bigcategoryid = "+petKind;
					keyMap.put("keywords", keywords);
				}else{
					session.removeAttribute("keywords");
				}
				
				
				
				System.out.println(" 搜索用sql： "+queryCount);
				
				allNumber = petService.query(queryCount).size();
				//计算最大页数
				maxPageNumber = allNumber%showNumber==0?allNumber/showNumber:allNumber/showNumber+1;
				
				pageBean.setAllNumber(allNumber);
				pageBean.setCurrentPageNumber(currentPageNumber);
				pageBean.setMaxPageNumber(maxPageNumber);
				pageBean.setShowNumber(showNumber);
				
				List<PetBean> pets = (List<PetBean>) petService.pageSearchQuery(petKind, showNumber, currentPageNumber,keyMap);
				
				
				//修改pageQuery方法
				//设置成可关键字查询的方法
				
				
				//进行相关处理
				String path = request.getContextPath();
				
				//通过获取主页传递的dogs来得到数据，如果为空的话就重新获取，不为空则利用
				//但是会发生不同步bug，后台修改但是前台并不会显示
				
				session.setAttribute("dogsFlag", "yes");
				session.setAttribute("pets", pets);
				session.setAttribute("pageBean", pageBean);
				
				response.sendRedirect(path+"/web/dogs.jsp");
				
				
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
